//
//  BLStore.m
//  ITC_iOS
//
//  Created by Benz.L on 03/09/2018.
//  Copyright © 2018 Benz.L. All rights reserved.
//

#import "BLStore.h"
#import "NSObject+Archiver.h"

@interface BLStore()
@property(nonatomic, strong) NSUserDefaults *defaults;
@end
@implementation BLStore

+ (instancetype)manager {
    static BLStore *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [self new];
    });
    return manager;
}

+ (NSString *)uniqueKey:(NSString * _Nonnull)key {
    return key?[NSString stringWithFormat:@"%@_%@", NSStringFromClass(self.class),key]:nil;
}

- (NSUserDefaults *)defaults {
    if (!_defaults) {
        _defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.benzl.userdefaults"];
    }
    return _defaults;
}

- (void)saveObject:(id)object forKey:(NSString * _Nonnull)key {
    [self.defaults setObject:object forKey:key];
}

- (void)removeObjectForKey:(NSString * _Nonnull)key {
    [self.defaults removeObjectForKey:key];
}

- (id)objectForKey:(NSString * _Nonnull)key {
    return [self.defaults objectForKey:key];
}

@end


@implementation BLStore(Chain)
+ (UniqueKeyBlock)uniqueKey {
    return ^NSString *(NSString *key) {
        return [self uniqueKey:key];
    };
}
@end

